package com.itzb.asone.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itzb.asone.admin.constant.AdminConstant;
import com.itzb.asone.admin.constant.AdminMessageConstant;
import com.itzb.asone.application.BaseController;
import com.itzb.asone.constant.model.SysGroupConstant;
import com.itzb.asone.constant.model.SysOrgConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysDict;
import com.itzb.asone.system.model.SysGroup;
import com.itzb.asone.system.model.SysGroupUser;
import com.itzb.asone.system.model.SysOrg;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.system.service.ISysGroupService;
import com.itzb.asone.system.service.ISysGroupUserService;
import com.itzb.asone.utils.GitId;
import com.itzb.asone.utils.JsonResult;
import com.itzb.asone.utils.PageResult;

@Controller
@RequestMapping(value = "sysgroup")
public class GroupController extends BaseController {
	
	private Logger logger = Logger.getLogger(getClass()); 
	
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISysDictService sysDictService;
	@Autowired
	private ISysGroupService sysGroupService;
	@Autowired
	private ISysGroupUserService sysGroupUserService;

	/**
	 * 显示数据字典列表
	 * @param map
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public String list(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		return "system/sysgroup/list";
	}

	/**
	 * 获取数据字典列表
	 * @param page
	 * @param pageSize
	 * @param order
	 * @param sort
	 * @param request
	 * @param response
     * @return
     */
	@ResponseBody
	@RequestMapping(value = "/getdata", method = { RequestMethod.POST })
	public PageResult getData(
			SysGroup SysGroup,
			@RequestParam(required = false, defaultValue = "0", value = "offset") int page,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int pageSize,
			@RequestParam(required = false, defaultValue = "asc", value = "order") String order,
			@RequestParam(required = false, defaultValue = "create_time", value = "sort") String sort,
			HttpServletRequest request,HttpServletResponse response
			) {
		PageResult pageResult = new PageResult();	
		Criteria<SysGroup> param = new Criteria<SysGroup>();
		param.setPageSize(pageSize).setStartIndex(page);
		param.setOrderBy(sort + " " + order);
		if(SysGroup.getOrgCode() != null && !SysGroup.getOrgCode().equals("")){
			param.addParam("orgCode", SysGroup.getOrgCode());
		}
		List<SysGroup> list = sysGroupService.queryPage(param);
		pageResult.setTotal(sysGroupService.queryPageCount(param));
		pageResult.setRows(sysDictService.translateToMapList(list));
		return pageResult;
	}

	/**
	 * 显示新增数据字典页面
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/showmodify", method = { RequestMethod.GET })
	public String showModify(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		SysGroup sysGroup = new SysGroup();
		if(id != null && !id.equals("")){
			sysGroup = sysGroupService.queryEntityById(id);
		}
		modelMap.put("sysGroup", sysGroup);
		return "system/sysgroup/modify";
	}

	/**
	 * 新增数据字典
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public JsonResult modify(SysGroup sysGroup,HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String showtype = request.getParameter("showtype");
		Criteria<SysGroup> param = new Criteria<SysGroup>();
		if(showtype.equals(AdminConstant.SHOW_TYPE_UPDATE)){
			param.addParam("id", sysGroup.getId());
			param.setRecord(sysGroup);
			sysGroupService.updateByCriteria(param);
		} else {
			sysGroup.setId(GitId.primaryKey());
			sysGroup.setGroupStatus(Integer.parseInt(SysGroupConstant.T_SYS_GROUP_STATUS_1));
			sysGroupService.insert(sysGroup);
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}
	
	/**
	 * 删除数据字典
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	public JsonResult del(HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String[] ids = request.getParameterValues("id[]");
		boolean flag = false;
		for(int i=0;i<ids.length;i++){
			Criteria<SysGroupUser> paramSysGroupUser = new Criteria<SysGroupUser>();
			paramSysGroupUser.addParam("groupId", ids[i]);
			if(sysGroupUserService.queryPage(paramSysGroupUser).size() > 0){
				flag = true;
				continue;
			}
			sysGroupService.deleteById(ids[i]);
			
		}
		if(flag){
			return jsonResult.declareFailure(messageService.message(AdminMessageConstant.SYSTEM_DEL_GROUP_1));
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}

}
